--@class HuanhuaFunc : 幻化之战相关的函数
local HuanhuaFunc = {}

--- 构建技能池
---@param room Room
function HuanhuaFunc.createSkillPool(room)
  local skill_pool = {}
  for _, g_name in ipairs(room.general_pile) do
    local general = Fk.generals[g_name]
    local skillNameList = general:getSkillNameList()
    
    -- 修正技能过滤逻辑
    skillNameList = table.filter(skillNameList, function(s) 
      local skill = Fk.skills[s]
      -- 检查技能是否存在且不是觉醒技、限定技等特殊技能
      if not skill then return false end
      
      -- 新版本中可能需要使用不同的属性来判断技能类型
      -- 暂时先过滤掉明显不是普通技能的技能
      if string.find(s, "awaken") or string.find(s, "limit") or string.find(s, "compulsory") then
        return false
      end
      
      -- 过滤掉规则类技能和标记技能
      if string.sub(s, 1, 1) == "#" then
        return false
      end
      
      return true
    end)
    
    table.insertTableIfNeed(skill_pool, skillNameList)
  end
  room:setTag("huanhua_skill_pool", skill_pool)
end

--- 获取指定数量的技能
---@param room Room
---@param num integer?
---@return string[]
function HuanhuaFunc.getSkills(room, num)
  num = num or 1
  local skills = {}
  local skill_pool = room:getTag("huanhua_skill_pool") or {}
  if #skill_pool == 0 or num > #skill_pool then 
    return {} 
  end
  
  for i = 1, num do
    local skill = table.random(skill_pool)
    table.removeOne(skill_pool, skill)
    table.insert(skills, skill)
  end
  room:setTag("huanhua_skill_pool", skill_pool)
  return skills
end

--- 将技能塞回技能池
---@param room Room
---@param skills string[]
function HuanhuaFunc.returnToSkillPool(room, skills)
  if #skills == 0 then return end
  local skill_pool = room:getTag("huanhua_skill_pool") or {}
  table.insertTable(skill_pool, skills)
  room:setTag("huanhua_skill_pool", skill_pool)
end

--- 进行幻化
---@param room Room  @ 所在房间
function HuanhuaFunc.doHuanhua(room)
  local alive_players = room.alive_players
  local skill_names = HuanhuaFunc.getSkills(room, 3 * #alive_players)
  
  if #skill_names == 0 then 
    -- 如果技能池空了，给所有玩家摸两张牌
    for _, p in ipairs(alive_players) do
      p:drawCards(2, "huanhua_choice")
    end
    return
  end

  local selected_skills = {}
  
  for i, p in ipairs(alive_players) do
    local choices = {}
    local start_idx = (i-1)*3 + 1
    local end_idx = math.min(i*3, #skill_names)
    local sliced_skills = table.slice(skill_names, start_idx, end_idx)
    
    table.insertTableIfNeed(choices, sliced_skills)
    table.insert(choices, "DrawTwo")

    if #choices > 1 then
      -- 使用 askForChoice 而不是 askToChooseGeneral
      local choice = room:askForChoice(p, choices, "huanhua_choice", "#huanhua-choose")
      
      if choice == "DrawTwo" then
        p:drawCards(2, "huanhua_choice")
      else
        room:handleAddLoseSkills(p, choice)
        table.insert(selected_skills, choice)
      end
    else
      -- 如果没有技能可选，默认摸牌
      p:drawCards(2, "huanhua_choice")
    end
  end
  
  -- 将未选择的技能返回技能池
  local unused_skills = table.filter(skill_names, function(s) 
    return not table.contains(selected_skills, s) 
  end)
  HuanhuaFunc.returnToSkillPool(room, unused_skills)
end

--- 处理单个玩家的遗产奖励
---@param room Room
---@param player ServerPlayer
function HuanhuaFunc.handleReward(room, player)
  local skill_names = HuanhuaFunc.getSkills(room, 3)
  if #skill_names == 0 then 
    player:drawCards(2, "huanhua_choice")
    return
  end
  
  local choices = {}
  table.insertTableIfNeed(choices, skill_names)
  table.insert(choices, "DrawTwo")

  if #choices > 1 then
    local choice = room:askForChoice(player, choices, "huanhua_choice", "#huanhua-choose")
    
    if choice == "DrawTwo" then
      player:drawCards(2, "huanhua_choice")
    else
      room:handleAddLoseSkills(player, choice)
    end
  else
    player:drawCards(2, "huanhua_choice")
  end
end

Fk:loadTranslationTable{
  ["#huanhua-choose"] = "幻化：选择以下一个技能获得，或摸两张牌",
  ["huanhua_choice"] = "幻化",
  ["DrawTwo"] = "摸两张牌",
  [":DrawTwo"] = "不要了，过牌才是硬道理！"
}

return HuanhuaFunc